#include "accountmanagemengt.h"
#include "ui_accountmanagemengt.h"

#include <QSqlQuery>
#include <QSqlRecord>
#include<QDebug>
#include <QSqlQueryModel>
#include <QMessageBox>
#include"database.h"
AccountManagemengt::AccountManagemengt(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AccountManagemengt)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/res/res_log.png"));
    setWindowTitle(tr("账户管理"));
    ui->pushButton->setText("新增");
    ui->pushButton_2->setText("删除");
    ui->pushButton_3->setText("修改");
    ui->pushButton_5->setText("复位密码");
    ui->pushButton_4->setText("提交");


    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//列随着表格变化而自适应变化
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);//行随着表格变化而自适应变化
    m_sqlModel  = new QSqlTableModel(this);
    m_sqlModel ->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_sqlModel ->setTable("UserInformation");
    m_sqlModel ->select(); //选取整个表的所有行
    ui->tableView->setModel(m_sqlModel );
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //表头显示设置
    m_sqlModel->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    m_sqlModel->setHeaderData(1, Qt::Horizontal, tr("密码"));
    m_sqlModel->setHeaderData(2, Qt::Horizontal, tr("角色"));
}

AccountManagemengt::~AccountManagemengt()
{
    delete ui;
}


void AccountManagemengt::on_pushButton_clicked()//新增
{
    while (m_sqlModel->canFetchMore())
    {
        m_sqlModel->fetchMore();
    }
    int rowCount = m_sqlModel->rowCount();
    m_sqlModel->insertRow(rowCount);// 在rowCount处添加一行
    m_sqlModel->setData(m_sqlModel->index(rowCount, 0), "");
    ui->tableView->scrollToBottom();  // 滚动到底部
}

void AccountManagemengt::on_pushButton_2_clicked()//删除
{

    auto index = ui->tableView->currentIndex(); // 找到当前行，然后removeRow
    m_sqlModel->removeRow(index.row());
    int ok = QMessageBox::warning(this, tr("删除当前行！"), tr("确定删除当前行？"), QMessageBox::Yes, QMessageBox::No);// 删除前，弹框确认
    if (ok == QMessageBox::No)
    {
        m_sqlModel->revertAll();
        return;
    }
    m_sqlModel->submitAll();  // 提交
    m_sqlModel->select(); // 查询
}

void AccountManagemengt::on_pushButton_3_clicked()//修改
{

    ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked); // 将表格的编辑模式打开
}

void AccountManagemengt::on_pushButton_4_clicked()//提交
{

    int ok = QMessageBox::warning(this, tr("提交修改"), tr("确定提交修改？"), QMessageBox::Yes, QMessageBox::No); // 提交弹框确认
    if (ok == QMessageBox::No)
    {
        qDebug() << "取消提交";
        return;
    }
    m_sqlModel->submitAll(); // 提交修改
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); // 将表格置为不可修改

}

void AccountManagemengt::on_pushButton_5_clicked()//复位密码
{
    int row = ui-> tableView ->currentIndex().row();//获得当前行索引
    QModelIndex index1 =m_sqlModel->index(row,1); //m_sqlModel是tableView里的模型，->index(行索引,列索引)
    QString id=m_sqlModel->data(index1).toString();//获取索引对应位置的数据转为字符串
    m_sqlModel->setData(m_sqlModel->index(row, 1), 8888888);
    //    m_sqlModel->submitAll();
}
